﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Application.MainModule.OrderManagement;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;

namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {
        public Order GetOrderByOrderId(Guid orderId)
        {
            try
            {
                IOrderManagementService orderService = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();

                return orderService.FindOrderById(orderId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public Order GetOrderById(Guid orderId)
        {
            try
            {
                IOrderManagementService orderService = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();

                return orderService.FindOrderById(orderId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeOrder(Order order)
        {
            try
            {
                IOrderManagementService orderService = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
                orderService.ChangeOrder(order);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveOrder(Order order)
        {
            try
            {
                IOrderManagementService orderService = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
                orderService.RemoveOrder(order);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddOrder(Order order)
        {
            try
            {
                IOrderManagementService orderService = IoCFactory.Instance.CurrentContainer.Resolve<IOrderManagementService>();
                orderService.AddOrder(order);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

    }
}
